package com.yhy.tradesystem.util;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.yhy.tradesystem.Const;
import com.yhy.tradesystem.TradeDataPak;
import com.yhy.tradesystem.TradeSystemFilter;

public class DataPakLogger implements TradeSystemFilter {
	
	private String logDataName = null;
	private String logFileName = null;
	
	public DataPakLogger(String logDataName, String logFileName) {
		super();
		this.logDataName = logDataName;
		this.logFileName = logFileName;
	}
	
	FileOutputStream fos;
	BufferedWriter bfw;
	public boolean openLog(){
		boolean ret = false;
		try {
			fos = new FileOutputStream(logFileName);
			ret = true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return ret;
	}
	
	public void closeLog(){
		if(fos!=null){
			try {
				fos.flush();
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void connectOutput(TradeSystemFilter filter) {
		// TODO Auto-generated method stub

	}

	@Override
	public void inputData(TradeDataPak data) {
		if(fos!=null){
			String log = String.format("%s,%f,%f\n",data.stockData.date.toGMTString(),data.pickValueFromTradeDataPak(Const.CLOSE),data.pickValueFromTradeDataPak(logDataName));
			try {
				fos.write(log.getBytes());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	@Override
	public TradeDataPak outputData() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void resetData() {
		// TODO Auto-generated method stub

	}

}
